<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT">










<meta name="description" content="JavaScript Standard Style这是 JavaScript standard 代码规范的全文。 掌握本规范的最好方法是安装并在自己的代码中使用它。 细则 使用两个空格进行缩进。 eslint: indent 123function hello (name) &amp;#123;  console.log(&apos;hi&apos;, name)&amp;#125;">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaScript Standard Style">
<meta property="og:url" content="http://changyuan.github.io/2017/07/11/JavaScript-Standard-Style/index.html">
<meta property="og:site_name" content="Chang Crazy&#39;s Blog">
<meta property="og:description" content="JavaScript Standard Style这是 JavaScript standard 代码规范的全文。 掌握本规范的最好方法是安装并在自己的代码中使用它。 细则 使用两个空格进行缩进。 eslint: indent 123function hello (name) &amp;#123;  console.log(&apos;hi&apos;, name)&amp;#125;">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2017-07-11T01:59:17.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="JavaScript Standard Style">
<meta name="twitter:description" content="JavaScript Standard Style这是 JavaScript standard 代码规范的全文。 掌握本规范的最好方法是安装并在自己的代码中使用它。 细则 使用两个空格进行缩进。 eslint: indent 123function hello (name) &amp;#123;  console.log(&apos;hi&apos;, name)&amp;#125;">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://changyuan.github.io/2017/07/11/JavaScript-Standard-Style/">





  <title>JavaScript Standard Style | Chang Crazy's Blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Chang Crazy's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">升级自己的操作系统</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-schedule">
          <a href="/schedule/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-calendar"></i> <br>
            
            日程表
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://changyuan.github.io/2017/07/11/JavaScript-Standard-Style/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="changyuan">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Chang Crazy's Blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">JavaScript Standard Style</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-07-11T09:59:17+08:00">
                2017-07-11
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv"><i class="fa fa-file-o"></i>
            <span class="busuanzi-value" id="busuanzi_value_page_pv"></span>
            </span>
          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="JavaScript-Standard-Style"><a href="#JavaScript-Standard-Style" class="headerlink" title="JavaScript Standard Style"></a>JavaScript Standard Style</h1><p>这是 JavaScript <a href="https://github.com/feross/standard" target="_blank" rel="noopener">standard</a> 代码规范的全文。</p>
<p>掌握本规范的最好方法是安装并在自己的代码中使用它。</p>
<h2 id="细则"><a href="#细则" class="headerlink" title="细则"></a>细则</h2><ul>
<li><p><strong>使用两个空格</strong>进行缩进。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/indent" target="_blank" rel="noopener"><code>indent</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">hello</span> (<span class="params">name</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">'hi'</span>, name)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<a id="more"></a>
</li>
<li><p>除需要转义的情况外，<strong>字符串统一使用单引号</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/quotes" target="_blank" rel="noopener"><code>quotes</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="string">'hello there'</span>)</span><br><span class="line">$(<span class="string">"&lt;div class='box'&gt;"</span>)</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>不要定义未使用的变量</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-unused-vars" target="_blank" rel="noopener"><code>no-unused-vars</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">myFunction</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">var</span> result = something()   <span class="comment">// ✗ avoid</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>关键字后面加空格</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/keyword-spacing" target="_blank" rel="noopener"><code>keyword-spacing</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (condition) &#123; ... &#125;   <span class="comment">// ✓ ok</span></span><br><span class="line"><span class="keyword">if</span>(condition) &#123; ... &#125;    <span class="comment">// ✗ avoid</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>函数声明时括号与函数名间加空格</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/space-before-function-paren" target="_blank" rel="noopener"><code>space-before-function-paren</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">name</span> (<span class="params">arg</span>) </span>&#123; ... &#125;   <span class="comment">// ✓ ok</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">name</span>(<span class="params">arg</span>) </span>&#123; ... &#125;    <span class="comment">// ✗ avoid</span></span><br><span class="line"></span><br><span class="line">run(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123; ... &#125;)      <span class="comment">// ✓ ok</span></span><br><span class="line">run(<span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123; ... &#125;)       <span class="comment">// ✗ avoid</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>始终使用</strong> <code>===</code> 替代 <code>==</code>。<br><br>例外： <code>obj == null</code> 可以用来检查 <code>null || undefined</code>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/eqeqeq" target="_blank" rel="noopener"><code>eqeqeq</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (name === <span class="string">'John'</span>)   <span class="comment">// ✓ ok</span></span><br><span class="line"><span class="keyword">if</span> (name == <span class="string">'John'</span>)    <span class="comment">// ✗ avoid</span></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (name !== <span class="string">'John'</span>)   <span class="comment">// ✓ ok</span></span><br><span class="line"><span class="keyword">if</span> (name != <span class="string">'John'</span>)    <span class="comment">// ✗ avoid</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>字符串拼接操作符 (Infix operators)</strong> 之间要留空格。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/space-infix-ops" target="_blank" rel="noopener"><code>space-infix-ops</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ✓ ok</span></span><br><span class="line"><span class="keyword">var</span> x = <span class="number">2</span></span><br><span class="line"><span class="keyword">var</span> message = <span class="string">'hello, '</span> + name + <span class="string">'!'</span></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">var</span> x=<span class="number">2</span></span><br><span class="line"><span class="keyword">var</span> message = <span class="string">'hello, '</span>+name+<span class="string">'!'</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>逗号后面加空格</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/comma-spacing" target="_blank" rel="noopener"><code>comma-spacing</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ✓ ok</span></span><br><span class="line"><span class="keyword">var</span> list = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>]</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">greet</span> (<span class="params">name, options</span>) </span>&#123; ... &#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">var</span> list = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>]</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">greet</span> (<span class="params">name,options</span>) </span>&#123; ... &#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>else 关键字要与花括号</strong>保持在同一行。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/brace-style" target="_blank" rel="noopener"><code>brace-style</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ✓ ok</span></span><br><span class="line"><span class="keyword">if</span> (condition) &#123;</span><br><span class="line">  <span class="comment">// ...</span></span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">  <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">if</span> (condition) &#123;</span><br><span class="line">  <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">else</span> &#123;</span><br><span class="line">  <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>多行 if 语句的</strong>的括号不能省。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/curly" target="_blank" rel="noopener"><code>curly</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ✓ ok</span></span><br><span class="line"><span class="keyword">if</span> (options.quiet !== <span class="literal">true</span>) <span class="built_in">console</span>.log(<span class="string">'done'</span>)</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ✓ ok</span></span><br><span class="line"><span class="keyword">if</span> (options.quiet !== <span class="literal">true</span>) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">'done'</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">if</span> (options.quiet !== <span class="literal">true</span>)</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">'done'</span>)</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>不要丢掉</strong>异常处理中<code>err</code>参数。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/handle-callback-err" target="_blank" rel="noopener"><code>handle-callback-err</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ✓ ok</span></span><br><span class="line">run(<span class="function"><span class="keyword">function</span> (<span class="params">err</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">if</span> (err) <span class="keyword">throw</span> err</span><br><span class="line">  <span class="built_in">window</span>.alert(<span class="string">'done'</span>)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ✗ avoid</span></span><br><span class="line">run(<span class="function"><span class="keyword">function</span> (<span class="params">err</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">window</span>.alert(<span class="string">'done'</span>)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>使用浏览器全局变量时加上</strong> <code>window.</code> 前缀。<br><br>Exceptions are: <code>document</code>, <code>console</code> and <code>navigator</code>.</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-undef" target="_blank" rel="noopener"><code>no-undef</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">window</span>.alert(<span class="string">'hi'</span>)   <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>不允许有连续多行空行</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-multiple-empty-lines" target="_blank" rel="noopener"><code>no-multiple-empty-lines</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ✓ ok</span></span><br><span class="line"><span class="keyword">var</span> value = <span class="string">'hello world'</span></span><br><span class="line"><span class="built_in">console</span>.log(value)</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">var</span> value = <span class="string">'hello world'</span></span><br></pre></td></tr></table></figure>
<p>console.log(value)</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">* **对于三元运算符** `?` 和 `:` 与他们所负责的代码处于同一行</span><br><span class="line"></span><br><span class="line">  eslint: [`operator-linebreak`](http://eslint.org/docs/rules/operator-linebreak)</span><br><span class="line"></span><br><span class="line">  ```js</span><br><span class="line">  // ✓ ok</span><br><span class="line">  var location = env.development ? &apos;localhost&apos; : &apos;www.api.com&apos;</span><br><span class="line"></span><br><span class="line">  // ✓ ok</span><br><span class="line">  var location = env.development</span><br><span class="line">    ? &apos;localhost&apos;</span><br><span class="line">    : &apos;www.api.com&apos;</span><br><span class="line"></span><br><span class="line">  // ✗ avoid</span><br><span class="line">  var location = env.development ?</span><br><span class="line">    &apos;localhost&apos; :</span><br><span class="line">    &apos;www.api.com&apos;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>每个 var 关键字</strong>单独声明一个变量。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/one-var" target="_blank" rel="noopener"><code>one-var</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ✓ ok</span></span><br><span class="line"><span class="keyword">var</span> silent = <span class="literal">true</span></span><br><span class="line"><span class="keyword">var</span> verbose = <span class="literal">true</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">var</span> silent = <span class="literal">true</span>, verbose = <span class="literal">true</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">var</span> silent = <span class="literal">true</span>,</span><br><span class="line">    verbose = <span class="literal">true</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>条件语句中赋值语句</strong>使用括号包起来。这样使得代码更加清晰可读，而不会认为是将条件判断语句的全等号（<code>===</code>）错写成了等号（<code>=</code>）。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-cond-assign" target="_blank" rel="noopener"><code>no-cond-assign</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ✓ ok</span></span><br><span class="line"><span class="keyword">while</span> ((m = text.match(expr))) &#123;</span><br><span class="line">  <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">while</span> (m = text.match(expr)) &#123;</span><br><span class="line">  <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>单行代码块两边加空格</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/block-spacing" target="_blank" rel="noopener"><code>block-spacing</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span> (<span class="params"></span>) </span>&#123;<span class="keyword">return</span> <span class="literal">true</span>&#125;    <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span> (<span class="params"></span>) </span>&#123; <span class="keyword">return</span> <span class="literal">true</span> &#125;  <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>对于变量和函数名统一使用驼峰命名法</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/camelcase" target="_blank" rel="noopener"><code>camelcase</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">my_function</span> (<span class="params"></span>) </span>&#123; &#125;    <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">myFunction</span> (<span class="params"></span>) </span>&#123; &#125;     <span class="comment">// ✓ ok</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> my_var = <span class="string">'hello'</span>           <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">var</span> myVar = <span class="string">'hello'</span>            <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>不允许有多余的行末逗号</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/comma-dangle" target="_blank" rel="noopener"><code>comma-dangle</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = &#123;</span><br><span class="line">  message: <span class="string">'hello'</span>,   <span class="comment">// ✗ avoid</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>始终将逗号置于行末</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/comma-style" target="_blank" rel="noopener"><code>comma-style</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = &#123;</span><br><span class="line">  foo: <span class="string">'foo'</span></span><br><span class="line">  ,<span class="attr">bar</span>: <span class="string">'bar'</span>   <span class="comment">// ✗ avoid</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> obj = &#123;</span><br><span class="line">  foo: <span class="string">'foo'</span>,</span><br><span class="line">  bar: <span class="string">'bar'</span>   <span class="comment">// ✓ ok</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>点号操作符须与属性需在同一行</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/dot-location" target="_blank" rel="noopener"><code>dot-location</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.</span><br><span class="line">  log(<span class="string">'hello'</span>)  <span class="comment">// ✗ avoid</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span></span><br><span class="line">  .log(<span class="string">'hello'</span>) <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>文件末尾留一空行</strong>。</p>
<p>elint: <a href="http://eslint.org/docs/rules/eol-last" target="_blank" rel="noopener"><code>eol-last</code></a></p>
</li>
<li><p><strong>函数调用时标识符与括号间不留间隔</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/func-call-spacing" target="_blank" rel="noopener"><code>func-call-spacing</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log (<span class="string">'hello'</span>) <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="string">'hello'</span>)  <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>键值对当中冒号与值之间要留空白</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/key-spacing" target="_blank" rel="noopener"><code>key-spacing</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = &#123; <span class="string">'key'</span> : <span class="string">'value'</span> &#125;    <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">var</span> obj = &#123; <span class="string">'key'</span> :<span class="string">'value'</span> &#125;     <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">var</span> obj = &#123; <span class="string">'key'</span>:<span class="string">'value'</span> &#125;      <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">var</span> obj = &#123; <span class="string">'key'</span>: <span class="string">'value'</span> &#125;     <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>构造函数要以大写字母开头</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/new-cap" target="_blank" rel="noopener"><code>new-cap</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">animal</span> (<span class="params"></span>) </span>&#123;&#125;</span><br><span class="line"><span class="keyword">var</span> dog = <span class="keyword">new</span> animal()    <span class="comment">// ✗ avoid</span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Animal</span> (<span class="params"></span>) </span>&#123;&#125;</span><br><span class="line"><span class="keyword">var</span> dog = <span class="keyword">new</span> Animal()    <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>无参的构造函数调用时要带上括号</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/new-parens" target="_blank" rel="noopener"><code>new-parens</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Animal</span> (<span class="params"></span>) </span>&#123;&#125;</span><br><span class="line"><span class="keyword">var</span> dog = <span class="keyword">new</span> Animal    <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">var</span> dog = <span class="keyword">new</span> Animal()  <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>对象中定义了存值器，一定要对应的定义取值器</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/accessor-pairs" target="_blank" rel="noopener"><code>accessor-pairs</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> person = &#123;</span><br><span class="line">  <span class="keyword">set</span> name (value) &#123;    <span class="comment">// ✗ avoid</span></span><br><span class="line">    <span class="keyword">this</span>.name = value</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> person = &#123;</span><br><span class="line">  <span class="keyword">set</span> name (value) &#123;</span><br><span class="line">    <span class="keyword">this</span>.name = value</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="keyword">get</span> name () &#123;         <span class="comment">// ✓ ok</span></span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>.name</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>子类的构造器中一定要调用 <code>super</code></strong></p>
<p>eslint: <a href="http://eslint.org/docs/rules/constructor-super" target="_blank" rel="noopener"><code>constructor-super</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Dog</span> </span>&#123;</span><br><span class="line">  <span class="keyword">constructor</span> () &#123;</span><br><span class="line">    <span class="keyword">super</span>()   <span class="comment">// ✗ avoid</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Dog</span> <span class="keyword">extends</span> <span class="title">Mammal</span> </span>&#123;</span><br><span class="line">  <span class="keyword">constructor</span> () &#123;</span><br><span class="line">    <span class="keyword">super</span>()   <span class="comment">// ✓ ok</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>使用数组字面量而不是构造器</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-array-constructor" target="_blank" rel="noopener"><code>no-array-constructor</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> nums = <span class="keyword">new</span> <span class="built_in">Array</span>(<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>)   <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">var</span> nums = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]            <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>避免使用 <code>arguments.callee</code> 和 <code>arguments.caller</code></strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-caller" target="_blank" rel="noopener"><code>no-caller</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span> (<span class="params">n</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">if</span> (n &lt;= <span class="number">0</span>) <span class="keyword">return</span></span><br><span class="line"></span><br><span class="line">  <span class="built_in">arguments</span>.callee(n - <span class="number">1</span>)   <span class="comment">// ✗ avoid</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span> (<span class="params">n</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">if</span> (n &lt;= <span class="number">0</span>) <span class="keyword">return</span></span><br><span class="line"></span><br><span class="line">  foo(n - <span class="number">1</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>避免对类名重新赋值</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-class-assign" target="_blank" rel="noopener"><code>no-class-assign</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Dog</span> </span>&#123;&#125;</span><br><span class="line">Dog = <span class="string">'Fido'</span>    <span class="comment">// ✗ avoid</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>避免修改使用 <code>const</code> 声明的变量</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-const-assign" target="_blank" rel="noopener"><code>no-const-assign</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> score = <span class="number">100</span></span><br><span class="line">score = <span class="number">125</span>       <span class="comment">// ✗ avoid</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>避免使用常量作为条件表达式的条件（循环语句除外）</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-constant-condition" target="_blank" rel="noopener"><code>no-constant-condition</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (<span class="literal">false</span>) &#123;    <span class="comment">// ✗ avoid</span></span><br><span class="line">  <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (x === <span class="number">0</span>) &#123;  <span class="comment">// ✓ ok</span></span><br><span class="line">  <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">while</span> (<span class="literal">true</span>) &#123;  <span class="comment">// ✓ ok</span></span><br><span class="line">  <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>正则中不要使用控制符</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-control-regex" target="_blank" rel="noopener"><code>no-control-regex</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> pattern = <span class="regexp">/\x1f/</span>    <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">var</span> pattern = <span class="regexp">/\x20/</span>    <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>不要使用 <code>debugger</code></strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-debugger" target="_blank" rel="noopener"><code>no-debugger</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">sum</span> (<span class="params">a, b</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">debugger</span>      <span class="comment">// ✗ avoid</span></span><br><span class="line">  <span class="keyword">return</span> a + b</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>不要对变量使用 <code>delete</code> 操作</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-delete-var" target="_blank" rel="noopener"><code>no-delete-var</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> name</span><br><span class="line"><span class="keyword">delete</span> name     <span class="comment">// ✗ avoid</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>不要定义冗余的函数参数</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-dupe-args" target="_blank" rel="noopener"><code>no-dupe-args</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">sum</span> (<span class="params">a, b, a</span>) </span>&#123;  <span class="comment">// ✗ avoid</span></span><br><span class="line">  <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">sum</span> (<span class="params">a, b, c</span>) </span>&#123;  <span class="comment">// ✓ ok</span></span><br><span class="line">  <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>类中不要定义冗余的属性</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-dupe-class-members" target="_blank" rel="noopener"><code>no-dupe-class-members</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Dog</span> </span>&#123;</span><br><span class="line">  bark () &#123;&#125;</span><br><span class="line">  bark () &#123;&#125;    <span class="comment">// ✗ avoid</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>对象字面量中不要定义重复的属性</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-dupe-keys" target="_blank" rel="noopener"><code>no-dupe-keys</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> user = &#123;</span><br><span class="line">  name: <span class="string">'Jane Doe'</span>,</span><br><span class="line">  name: <span class="string">'John Doe'</span>    <span class="comment">// ✗ avoid</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong><code>switch</code> 语句中不要定义重复的 <code>case</code> 分支</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-duplicate-case" target="_blank" rel="noopener"><code>no-duplicate-case</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">switch</span> (id) &#123;</span><br><span class="line">  <span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">    <span class="comment">// ...</span></span><br><span class="line">  <span class="keyword">case</span> <span class="number">1</span>:     <span class="comment">// ✗ avoid</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>同一模块有多个导入时一次性写完</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-duplicate-imports" target="_blank" rel="noopener"><code>no-duplicate-imports</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; myFunc1 &#125; <span class="keyword">from</span> <span class="string">'module'</span></span><br><span class="line"><span class="keyword">import</span> &#123; myFunc2 &#125; <span class="keyword">from</span> <span class="string">'module'</span>          <span class="comment">// ✗ avoid</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> &#123; myFunc1, myFunc2 &#125; <span class="keyword">from</span> <span class="string">'module'</span> <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>正则中不要使用空字符</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-empty-character-class" target="_blank" rel="noopener"><code>no-empty-character-class</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> myRegex = <span class="regexp">/^abc[]/</span>      <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">const</span> myRegex = <span class="regexp">/^abc[a-z]/</span>   <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>不要解构空值</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-empty-pattern" target="_blank" rel="noopener"><code>no-empty-pattern</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> &#123; <span class="attr">a</span>: &#123;&#125; &#125; = foo         <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">const</span> &#123; <span class="attr">a</span>: &#123; b &#125; &#125; = foo      <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>不要使用 <code>eval()</code></strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-eval" target="_blank" rel="noopener"><code>no-eval</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">eval</span>( <span class="string">"var result = user."</span> + propName ) <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">var</span> result = user[propName]             <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong><code>catch</code> 中不要对错误重新赋值</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-ex-assign" target="_blank" rel="noopener"><code>no-ex-assign</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">  <span class="comment">// ...</span></span><br><span class="line">&#125; <span class="keyword">catch</span> (e) &#123;</span><br><span class="line">  e = <span class="string">'new value'</span>             <span class="comment">// ✗ avoid</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">  <span class="comment">// ...</span></span><br><span class="line">&#125; <span class="keyword">catch</span> (e) &#123;</span><br><span class="line">  <span class="keyword">const</span> newVal = <span class="string">'new value'</span>  <span class="comment">// ✓ ok</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>不要扩展原生对象</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-extend-native" target="_blank" rel="noopener"><code>no-extend-native</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">Object</span>.prototype.age = <span class="number">21</span>     <span class="comment">// ✗ avoid</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>避免多余的函数上下文绑定</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-extra-bind" target="_blank" rel="noopener"><code>no-extra-bind</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> name = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  getName()</span><br><span class="line">&#125;.bind(user)    <span class="comment">// ✗ avoid</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> name = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">this</span>.getName()</span><br><span class="line">&#125;.bind(user)    <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>避免不必要的布尔转换</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-extra-boolean-cast" target="_blank" rel="noopener"><code>no-extra-boolean-cast</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> result = <span class="literal">true</span></span><br><span class="line"><span class="keyword">if</span> (!!result) &#123;   <span class="comment">// ✗ avoid</span></span><br><span class="line">  <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> result = <span class="literal">true</span></span><br><span class="line"><span class="keyword">if</span> (result) &#123;     <span class="comment">// ✓ ok</span></span><br><span class="line">  <span class="comment">// ...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>不要使用多余的括号包裹函数</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-extra-parens" target="_blank" rel="noopener"><code>no-extra-parens</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> myFunc = (<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123; &#125;)   <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">const</span> myFunc = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123; &#125;     <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong><code>switch</code> 一定要使用 <code>break</code> 来将条件分支正常中断</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-fallthrough" target="_blank" rel="noopener"><code>no-fallthrough</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">switch</span> (filter) &#123;</span><br><span class="line">  <span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">    doSomething()    <span class="comment">// ✗ avoid</span></span><br><span class="line">  <span class="keyword">case</span> <span class="number">2</span>:</span><br><span class="line">    doSomethingElse()</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">switch</span> (filter) &#123;</span><br><span class="line">  <span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">    doSomething()</span><br><span class="line">    <span class="keyword">break</span>           <span class="comment">// ✓ ok</span></span><br><span class="line">  <span class="keyword">case</span> <span class="number">2</span>:</span><br><span class="line">    doSomethingElse()</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">switch</span> (filter) &#123;</span><br><span class="line">  <span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">    doSomething()</span><br><span class="line">    <span class="comment">// fallthrough  // ✓ ok</span></span><br><span class="line">  <span class="keyword">case</span> <span class="number">2</span>:</span><br><span class="line">    doSomethingElse()</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>不要省去小数点前面的0</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-floating-decimal" target="_blank" rel="noopener"><code>no-floating-decimal</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> discount = <span class="number">.5</span>      <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">const</span> discount = <span class="number">0.5</span>     <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>避免对声明过的函数重新赋值</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-func-assign" target="_blank" rel="noopener"><code>no-func-assign</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">myFunc</span> (<span class="params"></span>) </span>&#123; &#125;</span><br><span class="line">myFunc = myOtherFunc    <span class="comment">// ✗ avoid</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>不要对全局只读对象重新赋值</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-global-assign" target="_blank" rel="noopener"><code>no-global-assign</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">window</span> = &#123;&#125;     <span class="comment">// ✗ avoid</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>注意隐式的 <code>eval()</code></strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-implied-eval" target="_blank" rel="noopener"><code>no-implied-eval</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">setTimeout(<span class="string">"alert('Hello world')"</span>)                   <span class="comment">// ✗ avoid</span></span><br><span class="line">setTimeout(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123; alert(<span class="string">'Hello world'</span>) &#125;)     <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>嵌套的代码块中禁止再定义函数</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-inner-declarations" target="_blank" rel="noopener"><code>no-inner-declarations</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (authenticated) &#123;</span><br><span class="line">  <span class="function"><span class="keyword">function</span> <span class="title">setAuthUser</span> (<span class="params"></span>) </span>&#123;&#125;    <span class="comment">// ✗ avoid</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>不要向 <code>RegExp</code> 构造器传入非法的正则表达式</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-invalid-regexp" target="_blank" rel="noopener"><code>no-invalid-regexp</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">RegExp</span>(<span class="string">'[a-z'</span>)    <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="built_in">RegExp</span>(<span class="string">'[a-z]'</span>)   <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>不要使用非法的空白符</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-irregular-whitespace" target="_blank" rel="noopener"><code>no-irregular-whitespace</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">myFunc</span> (<span class="params"></span>) /*&lt;<span class="title">NBSP</span>&gt;*/</span>&#123;&#125;   <span class="comment">// ✗ avoid</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>禁止使用 <code>__iterator__</code></strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-iterator" target="_blank" rel="noopener"><code>no-iterator</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">Foo.prototype.__iterator__ = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;&#125;   <span class="comment">// ✗ avoid</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>外部变量不要与对象属性重名</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-label-var" target="_blank" rel="noopener"><code>no-label-var</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> score = <span class="number">100</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">game</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  score: <span class="number">50</span>         <span class="comment">// ✗ avoid</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>不要使用标签语句</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-labels" target="_blank" rel="noopener"><code>no-labels</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">label:</span><br><span class="line">  <span class="keyword">while</span> (<span class="literal">true</span>) &#123;</span><br><span class="line">    <span class="keyword">break</span> label     <span class="comment">// ✗ avoid</span></span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>不要书写不必要的嵌套代码块</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-lone-blocks" target="_blank" rel="noopener"><code>no-lone-blocks</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">myFunc</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  &#123;                   <span class="comment">// ✗ avoid</span></span><br><span class="line">    myOtherFunc()</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">myFunc</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  myOtherFunc()       <span class="comment">// ✓ ok</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>不要混合使用空格与制表符作为缩进</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-mixed-spaces-and-tabs" target="_blank" rel="noopener"><code>no-mixed-spaces-and-tabs</code></a></p>
</li>
<li><p><strong>除了缩进，不要使用多个空格</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-multi-spaces" target="_blank" rel="noopener"><code>no-multi-spaces</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> id =    <span class="number">1234</span>    <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">const</span> id = <span class="number">1234</span>       <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>不要使用多行字符串</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-multi-str" target="_blank" rel="noopener"><code>no-multi-str</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> message = <span class="string">'Hello \</span></span><br><span class="line"><span class="string">                 world'</span>     <span class="comment">// ✗ avoid</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong><code>new</code> 创建对象实例后需要赋值给变量</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-new" target="_blank" rel="noopener"><code>no-new</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">new</span> Character()                     <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">const</span> character = <span class="keyword">new</span> Character()   <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>禁止使用 <code>Function</code> 构造器</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-new-func" target="_blank" rel="noopener"><code>no-new-func</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> sum = <span class="keyword">new</span> <span class="built_in">Function</span>(<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'return a + b'</span>)    <span class="comment">// ✗ avoid</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>禁止使用 <code>Object</code> 构造器</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-new-object" target="_blank" rel="noopener"><code>no-new-object</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> config = <span class="keyword">new</span> <span class="built_in">Object</span>()   <span class="comment">// ✗ avoid</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>禁止使用 <code>new require</code></strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-new-require" target="_blank" rel="noopener"><code>no-new-require</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> myModule = <span class="keyword">new</span> <span class="built_in">require</span>(<span class="string">'my-module'</span>)    <span class="comment">// ✗ avoid</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>禁止使用 <code>Symbol</code> 构造器</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-new-symbol" target="_blank" rel="noopener"><code>no-new-symbol</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> foo = <span class="keyword">new</span> <span class="built_in">Symbol</span>(<span class="string">'foo'</span>)   <span class="comment">// ✗ avoid</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>禁止使用原始包装器</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-new-wrappers" target="_blank" rel="noopener"><code>no-new-wrappers</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> message = <span class="keyword">new</span> <span class="built_in">String</span>(<span class="string">'hello'</span>)   <span class="comment">// ✗ avoid</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>不要将全局对象的属性作为函数调用</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-obj-calls" target="_blank" rel="noopener"><code>no-obj-calls</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> math = <span class="built_in">Math</span>()   <span class="comment">// ✗ avoid</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>不要使用八进制字面量</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-octal" target="_blank" rel="noopener"><code>no-octal</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> num = <span class="number">042</span>     <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">const</span> num = <span class="string">'042'</span>   <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>字符串字面量中也不要使用八进制转义字符</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-octal-escape" target="_blank" rel="noopener"><code>no-octal-escape</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> copyright = <span class="string">'Copyright \251'</span>  <span class="comment">// ✗ avoid</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>使用 <code>__dirname</code> 和 <code>__filename</code> 时尽量避免使用字符串拼接</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-path-concat" target="_blank" rel="noopener"><code>no-path-concat</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> pathToFile = __dirname + <span class="string">'/app.js'</span>            <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">const</span> pathToFile = path.join(__dirname, <span class="string">'app.js'</span>)   <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>使用 <code>getPrototypeOf</code> 来替代 <strong><code>__proto__</code></strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-proto" target="_blank" rel="noopener"><code>no-proto</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> foo = obj.__proto__               <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">const</span> foo = <span class="built_in">Object</span>.getPrototypeOf(obj)  <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>不要重复声明变量</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-redeclare" target="_blank" rel="noopener"><code>no-redeclare</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> name = <span class="string">'John'</span></span><br><span class="line"><span class="keyword">let</span> name = <span class="string">'Jane'</span>     <span class="comment">// ✗ avoid</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> name = <span class="string">'John'</span></span><br><span class="line">name = <span class="string">'Jane'</span>         <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>正则中避免使用多个空格</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-regex-spaces" target="_blank" rel="noopener"><code>no-regex-spaces</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> regexp = <span class="regexp">/test   value/</span>   <span class="comment">// ✗ avoid</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> regexp = <span class="regexp">/test &#123;3&#125;value/</span>  <span class="comment">// ✓ ok</span></span><br><span class="line"><span class="keyword">const</span> regexp = <span class="regexp">/test value/</span>     <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>return 语句中的赋值必需有括号包裹</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-return-assign" target="_blank" rel="noopener"><code>no-return-assign</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">sum</span> (<span class="params">a, b</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> result = a + b     <span class="comment">// ✗ avoid</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">sum</span> (<span class="params">a, b</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> (result = a + b)   <span class="comment">// ✓ ok</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>避免将变量赋值给自己</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-self-assign" target="_blank" rel="noopener"><code>no-self-assign</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">name = name   <span class="comment">// ✗ avoid</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>避免将变量与自己进行比较操作</strong>。</p>
<p>esint: <a href="http://eslint.org/docs/rules/no-self-compare" target="_blank" rel="noopener"><code>no-self-compare</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (score === score) &#123;&#125;   <span class="comment">// ✗ avoid</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>避免使用逗号操作符</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-sequences" target="_blank" rel="noopener"><code>no-sequences</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (doSomething(), !!test) &#123;&#125;   <span class="comment">// ✗ avoid</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>不要随意更改关键字的值</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-shadow-restricted-names" target="_blank" rel="noopener"><code>no-shadow-restricted-names</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> <span class="literal">undefined</span> = <span class="string">'value'</span>     <span class="comment">// ✗ avoid</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>禁止使用稀疏数组（Sparse arrays）</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-sparse-arrays" target="_blank" rel="noopener"><code>no-sparse-arrays</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> fruits = [<span class="string">'apple'</span>,, <span class="string">'orange'</span>]       <span class="comment">// ✗ avoid</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>不要使用制表符</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-tabs" target="_blank" rel="noopener"><code>no-tabs</code></a></p>
</li>
<li><p><strong>正确使用 ES6 中的字符串模板</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-template-curly-in-string" target="_blank" rel="noopener"><code>no-template-curly-in-string</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> message = <span class="string">'Hello $&#123;name&#125;'</span>   <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">const</span> message = <span class="string">`Hello <span class="subst">$&#123;name&#125;</span>`</span>   <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>使用 <code>this</code> 前请确保 <code>super()</code> 已调用</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-this-before-super" target="_blank" rel="noopener"><code>no-this-before-super</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Dog</span> <span class="keyword">extends</span> <span class="title">Animal</span> </span>&#123;</span><br><span class="line">  <span class="keyword">constructor</span> () &#123;</span><br><span class="line">    <span class="keyword">this</span>.legs = <span class="number">4</span>     <span class="comment">// ✗ avoid</span></span><br><span class="line">    <span class="keyword">super</span>()</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>用 <code>throw</code> 抛错时，抛出 <code>Error</code> 对象而不是字符串</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-throw-literal" target="_blank" rel="noopener"><code>no-throw-literal</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">throw</span> <span class="string">'error'</span>               <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">throw</span> <span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">'error'</span>)    <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>行末不留空格</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-trailing-spaces" target="_blank" rel="noopener"><code>no-trailing-spaces</code></a></p>
</li>
<li><p><strong>不要使用 <code>undefined</code> 来初始化变量</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-undef-init" target="_blank" rel="noopener"><code>no-undef-init</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> name = <span class="literal">undefined</span>    <span class="comment">// ✗ avoid</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> name</span><br><span class="line">name = <span class="string">'value'</span>          <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>循环语句中注意更新循环变量</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-unmodified-loop-condition" target="_blank" rel="noopener"><code>no-unmodified-loop-condition</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; items.length; j++) &#123;...&#125;    <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; items.length; i++) &#123;...&#125;    <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>如果有更好的实现，尽量不要使用三元表达式</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-unneeded-ternary" target="_blank" rel="noopener"><code>no-unneeded-ternary</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> score = val ? val : <span class="number">0</span>     <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">let</span> score = val || <span class="number">0</span>          <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong><code>return</code>，<code>throw</code>，<code>continue</code> 和 <code>break</code> 后不要再跟代码</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-unreachable" target="_blank" rel="noopener"><code>no-unreachable</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">doSomething</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">'never called'</span>)     <span class="comment">// ✗ avoid</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong><code>finally</code> 代码块中不要再改变程序执行流程</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-unsafe-finally" target="_blank" rel="noopener"><code>no-unsafe-finally</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">  <span class="comment">// ...</span></span><br><span class="line">&#125; <span class="keyword">catch</span> (e) &#123;</span><br><span class="line">  <span class="comment">// ...</span></span><br><span class="line">&#125; <span class="keyword">finally</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="number">42</span>     <span class="comment">// ✗ avoid</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>关系运算符的左值不要做取反操作</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-unsafe-negation" target="_blank" rel="noopener"><code>no-unsafe-negation</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (!key <span class="keyword">in</span> obj) &#123;&#125;       <span class="comment">// ✗ avoid</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>避免不必要的 <code>.call()</code> 和 <code>.apply()</code></strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-useless-call" target="_blank" rel="noopener"><code>no-useless-call</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sum.call(<span class="literal">null</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>)   <span class="comment">// ✗ avoid</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>避免使用不必要的计算值作对象属性</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-useless-computed-key" target="_blank" rel="noopener"><code>no-useless-computed-key</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> user = &#123; [<span class="string">'name'</span>]: <span class="string">'John Doe'</span> &#125;   <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">const</span> user = &#123; <span class="attr">name</span>: <span class="string">'John Doe'</span> &#125;       <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>禁止多余的构造器</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-useless-constructor" target="_blank" rel="noopener"><code>no-useless-constructor</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Car</span> </span>&#123;</span><br><span class="line">  <span class="keyword">constructor</span> () &#123;      <span class="comment">// ✗ avoid</span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>禁止不必要的转义</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-useless-escape" target="_blank" rel="noopener"><code>no-useless-escape</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> message = <span class="string">'Hell\o'</span>  <span class="comment">// ✗ avoid</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>import, export 和解构操作中，禁止赋值到同名变量</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-useless-rename" target="_blank" rel="noopener"><code>no-useless-rename</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; config <span class="keyword">as</span> config &#125; <span class="keyword">from</span> <span class="string">'./config'</span>     <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">import</span> &#123; config &#125; <span class="keyword">from</span> <span class="string">'./config'</span>               <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>属性前面不要加空格</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-whitespace-before-property" target="_blank" rel="noopener"><code>no-whitespace-before-property</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">user .name      <span class="comment">// ✗ avoid</span></span><br><span class="line">user.name       <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>禁止使用 <code>with</code></strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-with" target="_blank" rel="noopener"><code>no-with</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">with</span> (val) &#123;...&#125;    <span class="comment">// ✗ avoid</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>对象属性换行时注意统一代码风格</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/object-property-newline" target="_blank" rel="noopener"><code>object-property-newline</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> user = &#123;</span><br><span class="line">  name: <span class="string">'Jane Doe'</span>, <span class="attr">age</span>: <span class="number">30</span>,</span><br><span class="line">  username: <span class="string">'jdoe86'</span>            <span class="comment">// ✗ avoid</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> user = &#123; <span class="attr">name</span>: <span class="string">'Jane Doe'</span>, <span class="attr">age</span>: <span class="number">30</span>, <span class="attr">username</span>: <span class="string">'jdoe86'</span> &#125;    <span class="comment">// ✓ ok</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> user = &#123;</span><br><span class="line">  name: <span class="string">'Jane Doe'</span>,</span><br><span class="line">  age: <span class="number">30</span>,</span><br><span class="line">  username: <span class="string">'jdoe86'</span></span><br><span class="line">&#125;                                                                 <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>代码块中避免多余留白</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/padded-blocks" target="_blank" rel="noopener"><code>padded-blocks</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (user) &#123;</span><br><span class="line">                            <span class="comment">// ✗ avoid</span></span><br><span class="line">  <span class="keyword">const</span> name = getName()</span><br><span class="line"></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> (user) &#123;</span><br><span class="line">  <span class="keyword">const</span> name = getName()    <span class="comment">// ✓ ok</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>展开运算符与它的表达式间不要留空白</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/rest-spread-spacing" target="_blank" rel="noopener"><code>rest-spread-spacing</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">fn(... args)    <span class="comment">// ✗ avoid</span></span><br><span class="line">fn(...args)     <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>遇到分号时空格要后留前不留</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/semi-spacing" target="_blank" rel="noopener"><code>semi-spacing</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span> ;i &lt; items.length ;i++) &#123;...&#125;    <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; items.length; i++) &#123;...&#125;    <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>代码块首尾留空格</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/space-before-blocks" target="_blank" rel="noopener"><code>space-before-blocks</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (admin)&#123;...&#125;     <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">if</span> (admin) &#123;...&#125;    <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>圆括号间不留空格</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/space-in-parens" target="_blank" rel="noopener"><code>space-in-parens</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">getName( name )     <span class="comment">// ✗ avoid</span></span><br><span class="line">getName(name)       <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>一元运算符后面跟一个空格</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/space-unary-ops" target="_blank" rel="noopener"><code>space-unary-ops</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typeof</span>!admin        <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">typeof</span> !admin        <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>注释首尾留空格</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/spaced-comment" target="_blank" rel="noopener"><code>spaced-comment</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//comment           // ✗ avoid</span></span><br><span class="line"><span class="comment">// comment          // ✓ ok</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/*comment*/</span>         <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="comment">/* comment */</span>       <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>模板字符串中变量前后不加空格</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/template-curly-spacing" target="_blank" rel="noopener"><code>template-curly-spacing</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> message = <span class="string">`Hello, <span class="subst">$&#123; name &#125;</span>`</span>    <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">const</span> message = <span class="string">`Hello, <span class="subst">$&#123;name&#125;</span>`</span>      <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>检查 <code>NaN</code> 的正确姿势是使用 <code>isNaN()</code></strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/use-isnan" target="_blank" rel="noopener"><code>use-isnan</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (price === <span class="literal">NaN</span>) &#123; &#125;      <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">if</span> (<span class="built_in">isNaN</span>(price)) &#123; &#125;       <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>用合法的字符串跟 <code>typeof</code> 进行比较操作</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/valid-typeof" target="_blank" rel="noopener"><code>valid-typeof</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">typeof</span> name === <span class="string">'undefimed'</span>     <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">typeof</span> name === <span class="string">'undefined'</span>     <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>自调用匿名函数 (IIFEs) 使用括号包裹</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/wrap-iife" target="_blank" rel="noopener"><code>wrap-iife</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> getName = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123; &#125;()     <span class="comment">// ✗ avoid</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> getName = (<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123; &#125;())   <span class="comment">// ✓ ok</span></span><br><span class="line"><span class="keyword">const</span> getName = (<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123; &#125;)()   <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong><code>yield *</code> 中的 <code>*</code> 前后都要有空格</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/yield-star-spacing" target="_blank" rel="noopener"><code>yield-star-spacing</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">yield</span>* increment()    <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">yield</span> * increment()   <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
<li><p><strong>请书写优雅的条件语句（avoid Yoda conditions）</strong>。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/yoda" target="_blank" rel="noopener"><code>yoda</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (<span class="number">42</span> === age) &#123; &#125;    <span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="keyword">if</span> (age === <span class="number">42</span>) &#123; &#125;    <span class="comment">// ✓ ok</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="关于分号"><a href="#关于分号" class="headerlink" title="关于分号"></a>关于分号</h2><ul>
<li><p>不要使用分号。 (参见：<a href="http://blog.izs.me/post/2353458699/an-open-letter-to-javascript-leaders-regarding" target="_blank" rel="noopener">1</a>，<a href="http://inimino.org/%7Einimino/blog/javascript_semicolons" target="_blank" rel="noopener">2</a>，<a href="https://www.youtube.com/watch?v=gsfbh17Ax9I" target="_blank" rel="noopener">3</a>)</p>
<p>eslint: <a href="http://eslint.org/docs/rules/semi" target="_blank" rel="noopener"><code>semi</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">window</span>.alert(<span class="string">'hi'</span>)   <span class="comment">// ✓ ok</span></span><br><span class="line"><span class="built_in">window</span>.alert(<span class="string">'hi'</span>);  <span class="comment">// ✗ avoid</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>不要使用 <code>(</code>, <code>[</code>, or <code>`</code> 等作为一行的开始。在没有分号的情况下代码压缩后会导致报错，而坚持这一规范则可避免出错。</p>
<p>eslint: <a href="http://eslint.org/docs/rules/no-unexpected-multiline" target="_blank" rel="noopener"><code>no-unexpected-multiline</code></a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ✓ ok</span></span><br><span class="line">;(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">window</span>.alert(<span class="string">'ok'</span>)</span><br><span class="line">&#125;())</span><br><span class="line"></span><br><span class="line"><span class="comment">// ✗ avoid</span></span><br><span class="line">(<span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">  <span class="built_in">window</span>.alert(<span class="string">'ok'</span>)</span><br><span class="line">&#125;())</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ✓ ok</span></span><br><span class="line">;[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>].forEach(bar)</span><br><span class="line"></span><br><span class="line"><span class="comment">// ✗ avoid</span></span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>].forEach(bar)</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// ✓ ok</span></span><br><span class="line">;<span class="string">`hello`</span>.indexOf(<span class="string">'o'</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// ✗ avoid</span></span><br><span class="line"><span class="string">`hello`</span>.indexOf(<span class="string">'o'</span>)</span><br></pre></td></tr></table></figure>
<p>备注：上面的写法只能说聪明过头了。</p>
<p>相比更加可读易懂的代码，那些看似投巧的写法是不可取的。</p>
<p>譬如：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">;[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>].forEach(bar)</span><br></pre></td></tr></table></figure>
<p>建议的写法是：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> nums = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>]</span><br><span class="line">nums.forEach(bar)</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="拓展阅读"><a href="#拓展阅读" class="headerlink" title="拓展阅读"></a>拓展阅读</h2><ul>
<li><a href="http://blog.izs.me/post/2353458699/an-open-letter-to-javascript-leaders-regarding" target="_blank" rel="noopener">An Open Letter to JavaScript Leaders Regarding Semicolons</a></li>
<li><a href="http://inimino.org/~inimino/blog/javascript_semicolons" target="_blank" rel="noopener">JavaScript Semicolon Insertion – Everything you need to know</a></li>
</ul>
<h5 id="一个值得观看的视频："><a href="#一个值得观看的视频：" class="headerlink" title="一个值得观看的视频："></a>一个值得观看的视频：</h5><ul>
<li><a href="https://www.youtube.com/watch?v=gsfbh17Ax9I" target="_blank" rel="noopener">JavaScript 中的分号多余吗？- YouTube</a></li>
</ul>
<p>当前主流的代码压缩方案都是基于词法（AST-based）进行的，所以在处理无分号的代码时完全没有压力（何况 JavaScript 中分号本来就不是强制的）。</p>
<h5 id="一段摘抄自-“An-Open-Letter-to-JavaScript-Leaders-Regarding-Semicolons”-这篇文章的内容："><a href="#一段摘抄自-“An-Open-Letter-to-JavaScript-Leaders-Regarding-Semicolons”-这篇文章的内容：" class="headerlink" title="一段摘抄自 “An Open Letter to JavaScript Leaders Regarding Semicolons” 这篇文章的内容："></a>一段摘抄自 <em><a href="http://blog.izs.me/post/2353458699/an-open-letter-to-javascript-leaders-regarding" target="_blank" rel="noopener">“An Open Letter to JavaScript Leaders Regarding Semicolons”</a></em> 这篇文章的内容：</h5><blockquote>
<p>[自动化插入分号的做法]是安全可依赖的，而且其产出的代码能够在所有浏览器里很好地运行。 Closure compiler, yuicompressor, packer 还有 jsmin 都能正确地对这样的代码进行压缩处理。并没有任何性能相关的问题。</p>
<p>不得不说，Javascript 社区里的大牛们一直是错误的，并不能教给你最佳实践。真是让人忧伤啊。 我建议先弄清楚 JS 是怎样断句的（还有就是哪些地方看起来断了其实并没有），明白了这个后就可以随心写出漂亮的代码了。</p>
<p>一般来说， <code>\n</code> 表示语句结束，除非：</p>
<pre><code>1. 该语句有未闭合的括号， 数组字面量， 对象字面量 或者其他不能正常结束一条语句的情况（譬如，以 `.` 或 `,` 结尾）
2. 该语句是 `--` 或者 `++` （它会将后面的内容进行自增或减）
3. 该语句是 `for()`，`while()`，`do`，`if()` 或者 `else` 并且没有 `{`
4. 下一行以 `[`，`(`，`+`，`*`，`/`，`-`，`,`，`.` 或者其他只会单独出现在两块内容间的二元操作符。
</code></pre><p>第一条很容易理解。即使在 JSLint 中，也允许 JSON，构造器的括号中，以及使用 <code>var</code> 配合 <code>,</code> 结尾来声明多个变量等这些情中包含 <code>\n</code>。</p>
<p>第二条有点奇葩。 我还想不出谁会（除了这里用作讨论外）写出 <code>i\n++\nj</code> 这样的代码来，不过，顺便说一下，这种写法最后解析的结果是 <code>i; ++j</code>，而不是 <code>i++; j</code>。</p>
<p>第三条也容易理解。 <code>if (x)\ny()</code> 等价于 <code>if (x) { y() }</code>。解释器会向下寻找到代码块或一条语句为止。</p>
<p><code>;</code> 是条合法的 JavaScript 语句。所以 <code>if(x);</code> 等价于 <code>if(x){}</code>，表示 “如果 x 为真，什么也不做。” 这种写法在循环里面可以看到，就是当条件判断与条件更新是同一个方法的时候。 不常见，但也不至于没听说过吧。</p>
<p>第四条就是常见的 “看，说过要加分号！” 的情形。但这些情况可以通过在语句前面加上分号来解决，如果你确定该语句跟前面的没关系的话。举个例子，假如你想这样：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&gt; foo();</span><br><span class="line">&gt; [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>].forEach(bar);</span><br><span class="line">&gt;</span><br></pre></td></tr></table></figure>
</blockquote>
<blockquote>
<p>那么完全可以这样来写：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">&gt; foo()</span><br><span class="line">&gt; ;[<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>].forEach(bar)</span><br><span class="line">&gt;</span><br></pre></td></tr></table></figure>
</blockquote>
<blockquote>
<p>后者的好处是分号比较瞩目，一旦习惯后便再也不会看到以 <code>(</code> 和 <code>[</code> 开头又不带分号的语句了。</p>
</blockquote>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>分享即是成长</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechat-reward-image.png" alt="changyuan 微信支付">
        <p>微信支付</p>
      </div>
    

    

    

  </div>
</div>

      </div>
    

    
      <div>
        <ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者：</strong>
    changyuan
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="http://changyuan.github.io/2017/07/11/JavaScript-Standard-Style/" title="JavaScript Standard Style">http://changyuan.github.io/2017/07/11/JavaScript-Standard-Style/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>
    本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/3.0/" rel="external nofollow" target="_blank">CC BY-NC-SA 3.0</a> 许可协议。转载请注明出处！
  </li>
</ul>

      </div>
    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/03/15/login-more/" rel="next" title="关于登陆的那些事">
                <i class="fa fa-chevron-left"></i> 关于登陆的那些事
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/07/11/one2more-db-separation/" rel="prev" title="1对多类业务架构分割">
                1对多类业务架构分割 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/avatar.jpg" alt="changyuan">
            
              <p class="site-author-name" itemprop="name">changyuan</p>
              <p class="site-description motion-element" itemprop="description">不断升级自己的操作系统</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">69</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">1</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">26</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/changyuan" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-fab fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://twitter.com/changocean1" target="_blank" title="Twitter">
                      
                        <i class="fa fa-fw fa-fab fa-twitter"></i>Twitter</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="http://weibo.com/changyuan2011" target="_blank" title="Weibo">
                      
                        <i class="fa fa-fw fa-fab fa-weibo"></i>Weibo</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.douban.com/people/changyuanyuan" target="_blank" title="Zhihu">
                      
                        <i class="fa fa-fw fa-fab fa-quora"></i>Zhihu</a>
                  </span>
                
            </div>
          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-inline">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                Links
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="http://weibo.com/changyuan2011" title="Weibo" target="_blank">Weibo</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://github.com/changyuan" title="GitHub" target="_blank">GitHub</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#JavaScript-Standard-Style"><span class="nav-number">1.</span> <span class="nav-text">JavaScript Standard Style</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#细则"><span class="nav-number">1.1.</span> <span class="nav-text">细则</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#关于分号"><span class="nav-number">1.2.</span> <span class="nav-text">关于分号</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#拓展阅读"><span class="nav-number">1.3.</span> <span class="nav-text">拓展阅读</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#一个值得观看的视频："><span class="nav-number">1.3.0.0.1.</span> <span class="nav-text">一个值得观看的视频：</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#一段摘抄自-“An-Open-Letter-to-JavaScript-Leaders-Regarding-Semicolons”-这篇文章的内容："><span class="nav-number">1.3.0.0.2.</span> <span class="nav-text">一段摘抄自 “An Open Letter to JavaScript Leaders Regarding Semicolons” 这篇文章的内容：</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">changyuan</span>

  
</div>









<!-- 网站运行时间的设置 -->
<span id="timeDate">载入天数...</span>
<span id="times">载入时分秒...</span>
<script>
    var now = new Date();
    function createtime() {
        var grt= new Date("08/08/2014 00:00:00");
        now.setTime(now.getTime()+250);
        days = (now - grt ) / 1000 / 60 / 60 / 24; dnum = Math.floor(days);
        hours = (now - grt ) / 1000 / 60 / 60 - (24 * dnum); hnum = Math.floor(hours);
        if(String(hnum).length ==1 ){hnum = "0" + hnum;} minutes = (now - grt ) / 1000 /60 - (24 * 60 * dnum) - (60 * hnum);
        mnum = Math.floor(minutes); if(String(mnum).length ==1 ){mnum = "0" + mnum;}
        seconds = (now - grt ) / 1000 - (24 * 60 * 60 * dnum) - (60 * 60 * hnum) - (60 * mnum);
        snum = Math.round(seconds); if(String(snum).length ==1 ){snum = "0" + snum;}
        document.getElementById("timeDate").innerHTML = "本站已运行 "+dnum+" 天 ";
        document.getElementById("times").innerHTML = hnum + " 小时 " + mnum + " 分 " + snum + " 秒.";
    }
setInterval("createtime()",250);
</script>

        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

</body>
</html>
